"use strict";
$(function() {});
var app = new Vue({
    el: "#app",
    data: {
        userId: 0,
        pageFalg: !0,
        pageType: 1,
        goods: {},
        addressNum: 0,
        addressList: [],
        ad: {},
        from: {
            name: "",
            mobile: "",
            address: "",
            addressDesc: "",
            is_default: 0
        },
        saveAddressText: "确认新增",
        idx: 0,
        idxType: 1,
        userRemark: "",
        ticketFlag: !1,
        ticketText: "不开票",
        ticket: {
            invoiceCompany: "",
            invoiceTax: "",
            invoiceBank: "",
            invoiceAccount: ""
        },
        show1: !1,
        model1: "",
        district: window.YDUI_DISTRICT,
        switch1: !1,
        picked: "1"
    },
    created: function() {
        if ($.getStorage("goods") && null != $.getStorage("goods")) {
            var e = JSON.parse($.getStorage("goods"));
            this.goods = e,
            "" != $.getPara("userId") ? this.userId = parseInt($.getPara("userId")) : null != $.getStorage("userId") && (this.userId = parseInt($.getStorage("userId"))),
            this.getAddress()
        } else
            this.$dialog.toast({
                mes: "异常",
                timeout: 2e3
            })
    },
    methods: {
        result1: function(e) {
            this.model1 = e.itemName1 + " " + e.itemName2 + " " + e.itemName3
        },
        getAddress: function() {
            var s = this;
            $.ajax({
                type: "GET",
                url: "/api/user/address/list",
                data: {
                    userId: s.userId
                },
                success: function(e) {
                    if (200 == e.code) {
                        var i = e.data;
                        if (1 <= i.length) {
                            if (s.addressNum = i.length,
                            $.each(i, function(e, i) {
                                if (1 == i.is_default) {
                                    var t = {};
                                    t.address = i.address,
                                    t.city = i.city,
                                    t.district = i.district,
                                    t.id = i.id,
                                    t.is_default = i.is_default,
                                    t.is_icon = !0,
                                    t.mobile = i.mobile,
                                    t.name = i.name,
                                    t.province = i.province,
                                    s.ad = t
                                }
                            }),
                            !s.ad.address) {
                                var t = {};
                                t.address = i[0].address,
                                t.city = i[0].city,
                                t.district = i[0].district,
                                t.id = i[0].id,
                                t.is_default = i[0].is_default,
                                t.mobile = i[0].mobile,
                                t.name = i[0].name,
                                t.province = i[0].province,
                                s.ad = t
                            }
                            s.addressList = i
                        }
                    } else
                        s.$dialog.toast({
                            mes: e.msg,
                            timeout: 1500
                        })
                },
                error: function() {}
            })
        },
        showAddress: function(e) {
            this.pageType = e,
            this.saveAddressText = "确认新增",
            this.idxType = 1
        },
        editAddress: function(e) {
            var i = this;
            this.idx = e.id,
            this.saveAddressText = "确认编辑",
            this.idxType = 2,
            this.pageType = 3,
            this.from.name = e.name,
            this.from.addressDesc = e.address,
            this.from.mobile = e.mobile;
            var t = e.is_default;
            i.switch1 = 1 == t,
            i.model1 = e.province + " " + e.city + " " + e.district
        },
        delAddress: function(e) {
            var i = this
              , t = e.id;
            i.$dialog.confirm({
                title: "温馨提示",
                mes: "确定要删除收货地址？",
                opts: function() {
                    $.ajax({
                        type: "POST",
                        url: "/api/user/address/delete",
                        data: {
                            id: t
                        },
                        success: function(e) {
                            200 == e.code ? (i.$dialog.toast({
                                mes: "删除成功",
                                timeout: 1500
                            }),
                            i.getAddress()) : i.$dialog.toast({
                                mes: e.msg,
                                timeout: 1500
                            })
                        },
                        error: function() {
                            i.$dialog.toast({
                                mes: "请求失败",
                                timeout: 1500
                            })
                        }
                    })
                }
            })
        },
        showAccount: function(e) {
            var i = this;
            i.ad.province = e.province,
            i.ad.name = e.name,
            i.ad.mobile = e.mobile,
            i.ad.is_icon = !1,
            1 == e.is_default && (i.ad.is_icon = !0),
            i.ad.is_default = e.is_default,
            i.ad.id = e.id,
            i.ad.district = e.district,
            i.ad.city = e.city,
            i.ad.address = e.address,
            i.pageType = 1
        },
        saveAddress: function() {
            var i = this;
            if (console.log(i.model1.split(" ")),
            "" == i.from.name)
                i.$dialog.toast({
                    mes: "请填写收货人",
                    timeout: 1500
                });
            else if ("" == i.model1)
                i.$dialog.toast({
                    mes: "请选择收货地区",
                    timeout: 1500
                });
            else if ("" == i.from.addressDesc)
                i.$dialog.toast({
                    mes: "请填写详细地址",
                    timeout: 1500
                });
            else if ("" == i.from.mobile)
                i.$dialog.toast({
                    mes: "请输入手机号码",
                    timeout: 1500
                });
            else if (11 != i.from.mobile.length)
                i.$dialog.toast({
                    mes: "请填写正确的手机号码",
                    timeout: 2e3
                });
            else {
                var e = {};
                e.userId = i.userId,
                e.name = i.from.name,
                e.mobile = i.from.mobile,
                e.address = i.from.addressDesc;
                var t = i.model1.split(" ");
                e.province = t[0],
                t[1] ? e.city = t[1] : e.city = "",
                t[2] ? e.district = t[2] : e.district = "",
                e.isDefault = 0,
                i.switch1 && (e.isDefault = 1);
                var s = "/api/user/address/add";
                2 == i.idxType && (e.id = i.idx,
                s = "/api/user/address/save"),
                $.ajax({
                    type: "POST",
                    url: s,
                    data: e,
                    success: function(e) {
                        200 == e.code ? (i.$dialog.toast({
                            mes: "操作成功",
                            timeout: 2e3
                        }),
                        i.showAddress(2),
                        i.getAddress()) : i.$dialog.toast({
                            mes: e.msg,
                            timeout: 2e3
                        })
                    },
                    error: function() {
                        i.$dialog.toast({
                            mes: "请求失败",
                            timeout: 2e3
                        })
                    }
                })
            }
        },
        saveTicket: function(e) {
            var i = this;
            2 == e ? (this.ticketFlag = !1,
            this.pageType = 1,
            this.ticketText = "不开票") : "" == this.ticket.invoiceCompany ? i.$dialog.toast({
                mes: "请填写开票公司名称",
                timeout: 2e3
            }) : "" == this.ticket.invoiceTax ? i.$dialog.toast({
                mes: "请填写开票税号",
                timeout: 2e3
            }) : "" == this.ticket.invoiceBank ? i.$dialog.toast({
                mes: "请填写开户银行",
                timeout: 2e3
            }) : "" == this.ticket.invoiceAccount ? i.$dialog.toast({
                mes: "请填写银行账号",
                timeout: 2e3
            }) : (this.ticketFlag = !0,
            this.pageType = 1,
            this.ticketText = this.ticket.invoiceCompany)
        },
        //立即支付
        submitPayBtn: function() {
            var e = this;
            if (this.addressNum < 1)
                ;
            else {
                var i = {};
                i.userId = e.userId,
                i.addressId = e.ad.id,//地址id
                i.goodsId = e.goods.id,
                i.specId = e.goods.specId,
                i.purchaseNum = e.goods.num;//购买数量
                var t = parseInt(e.picked);
                i.payType = t,
                i.userRemark = e.userRemark,//备注
                e.ticketFlag ? (i.invoiceCompany = e.ticket.invoiceCompany,
                i.invoiceTax = e.ticket.invoiceTax,
                i.invoiceBank = e.ticket.invoiceBank,
                i.invoiceAccount = e.ticket.invoiceAccount) : (i.invoiceCompany = "",
                i.invoiceTax = "",
                i.invoiceBank = "",
                i.invoiceAccount = ""),
                
                e.createOrder(i)
            }
        },
        createOrder: function(e) {
            var i = this;
            console.log(e);
            $.ajax({
                type: "POST",
                url: "/api/order/create",
                data: e,
                success: function(e) {
                    200 == e.code ? i.noticeApp(e.data) : i.$dialog.toast({
                        mes: e.msg,
                        timeout: 2e3
                    })
                },
                error: function() {
                    i.$dialog.toast({
                        mes: "异常",
                        timeout: 2e3
                    })
                }
            })
        },
        noticeApp: function(e) {
            var i = {};
            i.orderCode = e.orderCode,
            i.payType = e.payType,
            i.orderId = e.orderId,
            /(Android)/i.test(navigator.userAgent) ? jsAndroid.appPay(JSON.stringify(i)) : /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent) && window.webkit.messageHandlers.appPay.postMessage(i)
        }
    }
});
